﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel.Syndication;
using System.Reflection;
using System.Web.Mvc;

namespace DotaHelp.Helpers
{
    [AttributeUsage(AttributeTargets.Method)]
    public class RssAttribute : Attribute
    {
        public static IEnumerable<SyndicationItem> Load(Controller control)
        {
            foreach (var type in Assembly.GetExecutingAssembly().GetTypes())
            {
                foreach (var method in type.GetMethods())
                {
                    var attr = (RssAttribute)method.GetCustomAttributes(typeof(RssAttribute), false).FirstOrDefault();
                    if (attr != null)
                    {
                        Controller obj = (Controller)Activator.CreateInstance(type);
                        obj.ControllerContext = control.ControllerContext;                      
                        var items = (IEnumerable<SyndicationItem>)method.Invoke(obj, new object[0]);

                        foreach (var item in items)
                        {
                            yield return item;
                        }
                    }
                }
            }
        }
    }
}